define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'fa/chapter/index' + location.search,
                    add_url: 'fa/chapter/add',
                    edit_url: 'fa/chapter/edit',
                    del_url: 'fa/chapter/del',
                    multi_url: 'fa/chapter/multi',
                    import_url: 'fa/chapter/import',
                    table: 'fa_chapter',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'comic.title', title: __('Comic.title'), operate: 'LIKE'},
                        {field: 'comic_id', title: '漫画ID', operate: 'LIKE', visible: false},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {
                            field: 'thumb',
                            title: __('Thumb'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.images,
                            events: Table.api.events.image,
                        },
                        {
                            field: 'intro',
                            title: __('Intro'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'is_vip',
                            title: '是否vip',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.toggle,
                            yes: "1",
                            no: "0",
                        },
                        {
                            field: 'created_at',
                            title: __('Created_at'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updated_at',
                            title: __('Updated_at'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'resource',
                                    title: "章节图片",
                                    text: "章节图片",
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'fa/chapter/resource',
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        zip: function () {
            Controller.api.bindevent();
        },
        resource: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
